package com.shy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shy.config.common.BaseQuery;
import com.shy.config.model.R;
import com.shy.config.query.ActivityList;
import com.shy.config.util.AuthUtils;
import com.shy.entity.Activity;
import com.shy.entity.ActivityRemark;
import com.shy.entity.User;
import com.shy.mapper.ActivityMapper;
import com.shy.mapper.ActivityRemarkMapper;
import com.shy.mapper.UserMapper;
import com.shy.service.ActivityService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "activity")
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ActivityRemarkMapper activityRemarkMapper;

    /**
     *  分页查询活动列表
     * @param current 当前页码
     * @param pageSize 每页显示条数
     * @return 分页对象
     */
    @Override
    public PageInfo<Activity> queryActivityListByPage(Integer current, Integer pageSize,ActivityList activityList) {
        //设置分页参数
        PageHelper.startPage(current, pageSize);
        //查询数据
        List<Activity> list = activityMapper.queryActivityListByPage(new BaseQuery(), activityList);
        //创建分页对象
        PageInfo<Activity> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 活动详情
     * @param activityId
     * @return
     */
    @Override
    public Activity queryActivityById(Integer activityId) {
        return activityMapper.queryUserDetailById(activityId);
    }

    /**
     * 活动添加
     * @param activity
     * @return
     */
    @CacheEvict(key = "'activity:all'")
    @Override
    public Boolean addActivity(Activity activity) {
        activity.setCreateBy(AuthUtils.getLoginSecurityUserId());
        activity.setCreateTime(new Date());
        activity.setOwnerId(AuthUtils.getLoginSecurityUserId());
        return activityMapper.insertSelective(activity)>0;
    }

    /**
     * 查询负责人列表
     * @return
     */
    @Override
    public List<User> getResponsiblePersons() {
        List<User> activity = userMapper.queryUserList();
        return activity;
    }
    /**
     * 编辑活动信息
     */
    @CacheEvict(key = "'activity:all'")
    @Override
    public Boolean modifyActivity(Activity activity) {
        activity.setEditBy(AuthUtils.getLoginSecurityUserId());
        activity.setEditTime(new Date());
        return activityMapper.updateByPrimaryKeySelective(activity)>0;
    }
    /**
     * 批量删除活动信息
     */
    @CacheEvict(key = "'activity:all'")
    @Override
    public Boolean batchActivityDelete(List<Integer> activityIds) {
        return activityMapper.batchAvtivityDelete(activityIds)>0;
    }
    /**
     * 查询备注列表
     */
    @Override
    public List<Activity> queryUserListById(Integer activityId) {
        return activityRemarkMapper.queryUserListById(activityId);
    }
    /**
     * 删除活动备注
     */
    @Override
    public Boolean deleteRemark(Integer activityRemarkId) {
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(activityRemarkId);
        activityRemark.setDeleted(1);
        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark)>0;
    }
    /**
     * 添加活动备注
     */
    @Override
    public Boolean addActivityRemarks(ActivityRemark activityRemark) {
        activityRemark.setDeleted(0);
        activityRemark.setCreateBy(AuthUtils.getLoginSecurityUserId());
        //设置创建时间
        activityRemark.setCreateTime(new Date());
        return activityRemarkMapper.insertSelective(activityRemark)>0;
    }

    /**
     * 查询活动列表
     * @return
     */
    @Cacheable(key = "'activity:all'")
    @Override
    public List<Activity> queryActivityList() {
        return activityMapper.selectAllList();
    }
}
